<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/20
 * Time: 16:53
 */

namespace app\admin\controller;



use app\admin\model\SystemManager;
use app\admin\model\SystemNode;
use app\common\controller\AdminController;

use app\common\controller\Base;

class Login extends Base
{

    public function index(){
        return $this->fetch(':login');
    }

    public function login(){
        $userName = $this->request->post('userName');
        $passWord = $this->request->post('passWord');
        $manager = SystemManager::get(['username'=>$userName]);

        if(empty($manager)){
            $msg = '用户名或者密码错误';
        }elseif($manager->status == SystemManager::STATUS_BAN){
            $msg = '该账号已经被禁用';
        }elseif($manager->password === md5(md5($passWord))){
            session('userId', $manager->id);
            session('userName', $manager->real_name);
            $systemNode = new SystemNode();
            $nodeData = $systemNode->getUserNodes($manager->typeid);
            session('menus', $nodeData['menus']);
            session('rules', $nodeData['rules']);
            //更新管理员
            $manager->last_login_ip = $this->request->ip();
            $manager->last_login_time = date('Y-m-d H:i:s');
            $manager->loginnum++;
            $manager->save();
            return $this->jsonSuccess('登录成功,页面跳转中...',['url'=>'/index/index']);
        }else{
            $msg = '用户名或者密码错误';
        }
        return $this->jsonFail($msg);
    }

    public function logout(){
        session('userId', null);
        session('userName', null);
        return $this->jsonSuccess('退出登录成功',['url'=>'/login/index']);
    }

}
